package veterinaria.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import veterinaria.excepcion.DAOExcepcion;
import veterinaria.modelo.Dueno;
import veterinaria.util.ConexionBD;

public class DuenoDAO extends BaseDAO {

	public Collection<Dueno> buscarPorNombre(String nombre)
			throws DAOExcepcion {
		String query = "select idCliente,nombre,paterno,materno,correo,direccion,imaperfil,telefonoCasa,telefonoCelular,rucdni from Cliente where concat(nombre ,' ',paterno ,' ', materno) like ?";
		Collection<Dueno> lista = new ArrayList<Dueno>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, nombre + "%");
			rs = stmt.executeQuery();
			
			while (rs.next()) {
				Dueno vo = new Dueno();
				vo.setIdDueno(rs.getInt(1));
				vo.setNombre(rs.getString(2));
				vo.setPaterno(rs.getString(3));
				vo.setMaterno(rs.getString(4));
				vo.setEmail(rs.getString(5));
				vo.setDireccion(rs.getString(6));
				vo.setFoto(rs.getString(7));
				vo.setTelefono(rs.getString(8));
				vo.setCelular(rs.getString(9));
				vo.setRucdni(rs.getString(10));
				
				lista.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}

	public Dueno insertar(Dueno vo) throws DAOExcepcion {
		String query = "insert into Cliente(nombre,paterno,materno,correo,direccion,imaperfil,telefonoCasa,telefonoCelular,rucdni) values (?,?,?,?,?,?,?,?,?)";
		String query2= null;
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {		
			//Insertar
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);

			stmt.setString(1, vo.getNombre());
			stmt.setString(2, vo.getPaterno());
			stmt.setString(3, vo.getMaterno());
			stmt.setString(4, vo.getEmail());			
			stmt.setString(5, vo.getDireccion());
			stmt.setString(6, vo.getFoto());
			stmt.setString(7, vo.getTelefono());
			stmt.setString(8, vo.getCelular());
			stmt.setString(9, vo.getRucdni());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}

			// Obtener el ultimo id
			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			vo.setIdDueno(id);
			
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Dueno obtener(String atributo,Object param) throws DAOExcepcion {
		Dueno vo = new Dueno();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query ="select idCliente,nombre,paterno,materno,correo,direccion,imaperfil,telefonoCasa,telefonoCelular,rucdni from Cliente where ";
			
			
			
			if ("CODIGO".equals(atributo))
				query = query + "idCliente=?";
			if ("NOMBRE".equals(atributo))
				query = query + "concat(nombre ,' ',paterno ,' ', materno)=?";
			if ("DOCUMENTO".equals(atributo))
				query = query + "rucdni=?";
			
			
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);

			
			
			if ("CODIGO".equals(atributo))
				stmt.setInt(1, (int)param);
			if ("NOMBRE".equals(atributo) || "DOCUMENTO".equals(atributo))
				stmt.setString(1, (String) param);
				
			rs = stmt.executeQuery();
			
			if (rs.next()) {
				vo.setIdDueno(rs.getInt(1));
				vo.setNombre(rs.getString(2));
				vo.setPaterno(rs.getString(3));
				vo.setMaterno(rs.getString(4));
				vo.setEmail(rs.getString(5));
				vo.setDireccion(rs.getString(6));
				vo.setFoto(rs.getString(7));
				vo.setTelefono(rs.getString(8));
				vo.setCelular(rs.getString(9));
				vo.setRucdni(rs.getString(10));

			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idDueno) throws DAOExcepcion {
		String query = "delete from Cliente WHERE idCliente=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idDueno);
			int i = stmt.executeUpdate();

			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Dueno actualizar(Dueno vo) throws DAOExcepcion {
		String query = "update Cliente set nombre=?,paterno=?,materno=?,correo=?,direccion=?,imaperfil=?,telefonoCasa=?,telefonoCelular=?,rucdni=? where idCliente=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);

			stmt.setString(1, vo.getNombre());
			stmt.setString(2, vo.getPaterno());
			stmt.setString(3, vo.getMaterno());
			stmt.setString(4, vo.getEmail());	
			stmt.setString(5, vo.getDireccion());
			stmt.setString(6, vo.getFoto());
			stmt.setString(7, vo.getTelefono());
			stmt.setString(8, vo.getCelular());
			stmt.setString(9, vo.getRucdni());
			stmt.setInt(10, vo.getIdDueno());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public Collection<Dueno> listar() throws DAOExcepcion {
		Collection<Dueno> c = new ArrayList<Dueno>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "select idCliente,nombre,paterno,materno,correo,direccion,imaperfil,telefonoCasa,telefonoCelular,rucdni from Cliente order by concat(nombre ,' ',paterno ,' ', materno)";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Dueno vo = new Dueno();
				vo.setIdDueno(rs.getInt("idCliente"));
				vo.setNombre(rs.getString("nombre"));
				vo.setPaterno(rs.getString("paterno"));
				vo.setMaterno(rs.getString("materno"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}


}
